const callGenerateSql = function (str) {
    let sqlLabel = 'Preparing: '
    let paramsLabel = 'Parameters: '
    let sql
    let paramsStr
    let list = str.split('\n')
    for (let i = 0, len = list.length; i < len; i++) {
        let line = list[i]
        if (!sql && line.indexOf(sqlLabel) > -1) {
            sql = line.substring(line.indexOf(sqlLabel) + sqlLabel.length);
        }
        if (!paramsStr && line.indexOf(paramsLabel) > -1) {
            paramsStr = line.substring(line.indexOf(paramsLabel) + paramsLabel.length);
        }
        if (sql && paramsStr) {
            break
        }
    }

    let params = new Map();
    let paramIndex = 0
    while (true) {
        let typeIndex = indexOfSqlType(paramsStr)
        let index = typeIndex.index
        if (typeIndex <= -1 || index <= -1) {
            break
        }
        let type = typeIndex.type
        let value = paramsStr.substring(0, index)
        if (numSqlTypes.includes(type)) {
            value = value.replace(/'/g, '')
        } else {
            value = "'" + value + "'"
        }
        params.set(paramIndex++, value)

        paramsStr = paramsStr.substring(index + type.length)
        let typeLabel = ', '
        if (paramsStr.startsWith(typeLabel)) {
            paramsStr = paramsStr.substring(typeLabel.length)
        }
    }


    console.log(sql)
    console.log(params)

    let result = ''
    if (sql) {
        let list = sql.split('?');
        for (let i = 0, len = list.length; i < len; i++) {
            result = result.concat(list[i])
            if (i < len - 1) {
                result = result.concat(params.get(i))
            }
        }
    }

    return result.replace(/\\n/g, '\n')
}

const indexOfSqlType = function (str) {
    if (!str) {
        return -1;
    }
    let typeIndexMap = new Map();
    for (let i = 0; i < sqlTypes.length; i++) {
        let type = sqlTypes[i]
        let currentIndex = str.indexOf(type)
        if (currentIndex > -1) {
            typeIndexMap.set(currentIndex, type)
        }
    }

    let type = ''
    let minIndex = -1
    for (let [key, value] of typeIndexMap.entries()) {
        if (minIndex === -1) {
            if (key > minIndex) {
                minIndex = key
                type = value
            }
        } else {
            if (key < minIndex) {
                minIndex = key
                type = value
            }
        }
    }

    return {
        type: type,
        index: minIndex
    }
}

const matchMonth = function (str) {
    if (!str) {
        return '';
    }
    switch (str) {
        case 'Jan':
            return '01'
        case 'Feb':
            return '02'
        case 'Mar':
            return '03'
        case 'Apr':
            return '04'
        case 'May':
            return '05'
        case 'Jun':
            return '06'
        case 'Jul':
            return '07'
        case 'Aug':
            return '08'
        case 'Sep':
            return '09'
        case 'Oct':
            return '10'
        case 'Nov':
            return '11'
        case 'Dec':
            return '12'
    }
}
const matchGenerateSql = function (str) {
    return (str.indexOf("select") > -1
            || str.indexOf("SELECT") > -1
            || str.indexOf("insert") > -1
            || str.indexOf("INSERT") > -1
            || str.indexOf("update") > -1
            || str.indexOf("UPDATE") > -1
            || str.indexOf("delete") > -1
            || str.indexOf("DELETE") > -1)
        && str.indexOf("Preparing:") > -1
}

let sqlTypes = [
    '(ARRAY)',
    '(VARCHAR)',
    '(CHAR)',
    '(DATE)',
    '(BIGINT)',
    '(TIME)',
    '(TIMESTAMP)',
    '(TINYINT)',
    '(VARBINARY)',
    '(BINARY)',
    '(BIT)',
    '(BLOB)',
    '(BOOLEAN)',
    '(CLOB)',
    '(DATALINK)',
    '(DECIMAL)',
    '(DISTINCT)',
    '(DOUBLE)',
    '(FLOAT)',
    '(INTEGER)',
    '(JAVA_OBJECT)',
    '(LONGNVARCHAR)',
    '(LONGVARBINARY)',
    '(NCHAR)',
    '(NCLOB)',
    '(NULL)',
    '(NUMERIC)',
    '(NVARCHAR)',
    '(REAL)',
    '(REF)',
    '(ROWID)',
    '(SMALLINT)',
    '(SQLXML)',
    '(STRUCT)',
    '(OTHER)',

    '(null)',
    '(String)',
    '(Boolean)',
    '(Long)',
    '(Integer)',
    '(Double)',
    '(Timestamp)'
]
let numSqlTypes = [
    '(BIGINT)',
    '(TINYINT)',
    '(BOOLEAN)',
    '(DOUBLE)',
    '(FLOAT)',
    '(INTEGER)',
    '(NULL)',

    '(null)',
    '(Boolean)',
    '(Long)',
    '(Integer)',
    '(Double)'
]

module.exports = {
    callGenerateSql,
    matchGenerateSql
}